package com.fbty.mt.action.user;

import java.util.Date;

import com.fbty.base.action.BaseAction;
import com.fbty.mt.domain.CheckInfo;
import com.fbty.mt.domain.IpInfo;
import com.fbty.mt.domain.OnlyIpInfo;
import com.fbty.mt.domain.UserInfo;
import com.fbty.mt.domain.UserIpInfo;
import com.fbty.mt.service.IpService;
import com.fbty.mt.service.ProxyService;
import com.fbty.mt.service.UserIpInfoService;
import com.fbty.mt.utils.IPInfoHelper;

public class SendAction extends BaseAction {
	
	private Integer ip;

	public void setIp(Integer ip) {
		this.ip = ip;
	}
	private Integer id;
	
	private IpService ipInfoService;
	
	private ProxyService proxyService;
	
	private Boolean flag;
	
	private String speed;
	
	private String time;
	
	private Integer state;
	
	private boolean only;
	
	private UserIpInfoService userIpInfoService;
	/*
	 *检测ip是否可用，获取延迟和可用状态 
	 * 
	 */
	public String execute() throws Exception{
		if(flag){
			IpInfo ipInfo = ipInfoService.findIpInfoById(ip);
			Integer  type = 0;
			if("socks5".equalsIgnoreCase(ipInfo.getType())) {
				type = CheckInfo.SOCKS5_PROXY;
			} else if("socks4".equalsIgnoreCase(ipInfo.getType())) {
				type = CheckInfo.SOCKS4_PROXY;
			} else if("http".equalsIgnoreCase(ipInfo.getType())) {
				type = CheckInfo.HTTP_PROXY;
			} else if("https".equalsIgnoreCase(ipInfo.getType())) {
				type = CheckInfo.HTTPS_PROXY;
			}
			try{
				CheckInfo c=proxyService.checkConnection(ipInfo.getIp(), Integer.parseInt(ipInfo.getPort()), type);
				if(c.getReslut() == 0) {
					ipInfo.setSpeed((double)c.getDelay()/1000+"");
					ipInfo.setUpdateTime(new Date());
					ipInfo.setState(1);
				}else{
					ipInfo.setSpeed("--");
					ipInfo.setUpdateTime(new Date());
					ipInfo.setState(2);
				}
			}catch(Exception e){
				ipInfo.setSpeed("--");
				ipInfo.setUpdateTime(new Date());
				ipInfo.setState(2);
			}
			ipInfoService.modifyIpInfo(ipInfo);
			//不将ip信息返回
			time = ipInfo.getTime();
			state = ipInfo.getState();
			speed = ipInfo.getSpeed();
		}else{//独享
			OnlyIpInfo info = ipInfoService.findOnlyIpInfoById(ip);
			
			UserIpInfo	uif = userIpInfoService.findUserIpById(id);
			
			if(uif.getEndTime()<System.currentTimeMillis()){
				state = -1;
				time = info.getTime();
				speed = info.getSpeed();
				return "success";
			}
			
			try{
			CheckInfo c=proxyService.checkConnection(info.getEntranceIp(),info.getEntrancePort(),uif.getUsername(),uif.getPassword());
				if(c.getReslut() == 0) {
					info.setSpeed((double)c.getDelay()/1000+"");
					info.setUploadTime(new Date());
					info.setUsed(1);
				}else{
					info.setSpeed("--");
					info.setUploadTime(new Date());
					info.setUsed(2);
					if(only){
						uif.setNum(uif.getNum()+1);
						userIpInfoService.modifyUserIpInfo(uif);
					}
				}
			}catch(Exception e){
				e.printStackTrace();
				info.setSpeed("--");
				info.setUploadTime(new Date());
				info.setUsed(2);
				if(only){
					try {
						uif.setNum(uif.getNum()+1);
						userIpInfoService.modifyUserIpInfo(uif);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}			
			ipInfoService.modifyIpInfo(info);
			state = info.getUsed();
			time = info.getTime();
			speed = info.getSpeed();
		}
		return "success";
	}
	
	public void setIpInfoService(IpService ipInfoService) {
		this.ipInfoService = ipInfoService;
	}

	public void setProxyService(ProxyService proxyService) {
		this.proxyService = proxyService;
	}

	public void setFlag(Boolean flag) {
		this.flag = flag;
	}

	public String getSpeed() {
		return speed;
	}

	public String getTime() {
		return time;
	}

	public Integer getState() {
		return state;
	}

	public void setOnly(boolean only) {
		this.only = only;
	}

	public void setUserIpInfoService(UserIpInfoService userIpInfoService) {
		this.userIpInfoService = userIpInfoService;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	
	
	
}
